package com.skstudio.nuprint.lpd;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.skstudio.nuprint.PsFileMonitorService;

import android.annotation.SuppressLint;
import android.os.Environment;

@SuppressLint("DefaultLocale")
public class LpdQueue extends Thread {

	public interface OnDocumentListener {
		public void onNewDocument(LpdPrintJob job);
	}

	OnDocumentListener mListener = null;

	public void setListener(OnDocumentListener listener) {
		mListener = listener;
	}

	final static LpdQueue cQueue = new LpdQueue();
	static int mJobId = 0;

	protected LpdQueue() {

	}

	public static LpdQueue getInstance() {
		return cQueue;
	}

	public synchronized void addPrintJob(LpdPrintJob job) {
		mJobs.add(job);
	}

	ArrayList<LpdPrintJob> mJobs = new ArrayList<LpdPrintJob>();

	protected synchronized List<LpdPrintJob> getJobs(String queueName) {
		ArrayList<LpdPrintJob> list = new ArrayList<LpdPrintJob>();
		for (LpdPrintJob job : mJobs) {
			if (job.mQueueName.equalsIgnoreCase(queueName)) {
				list.add(job);
			}
		}
		return list;
	}

	public synchronized void startPrint(String queueName) {

	}

	public synchronized void removePrintJob(String queueName, String agent,
			String options) {
		for (LpdPrintJob job : mJobs) {
			if (job.mQueueName.equalsIgnoreCase(queueName)) {
				if (agent != null) {
					// TODO:
				} else {
					mJobs.remove(job);
					return;
				}
			}
		}
	}

	public synchronized List<LpdPrintJob> getPrintJobs(String queueName,
			String options) {
		List<LpdPrintJob> list = new ArrayList<LpdPrintJob>();
		for (LpdPrintJob job : mJobs) {
			if (job.mQueueName.equalsIgnoreCase(queueName))
				list.add(job);
		}
		return list;
	}

	int mRunState = 0;

	@Override
	public void run() {
		while (mRunState == 0) {
			if (mJobs.size() == 0) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
			}

			try {

				File pubRoot = new File(
						Environment.getExternalStorageDirectory(),
						PsFileMonitorService.APP_ROOT);
				LpdPrintJob job = mJobs.remove(0);

				// write content
				String path = String.format("%s/job_%05d.ps",
						pubRoot.getAbsoluteFile(), mJobId);
				FileUtils.writeByteArrayToFile(new File(path),
						job.mDataFile.contents, false);

				// write meta
				path = String.format("%s/job_%05d.txt",
						pubRoot.getAbsoluteFile(), mJobId);
				PrintWriter pw = new PrintWriter(path);
				StringBuilder sb = new StringBuilder();
				job.getCtrlFile().write(sb);
				pw.write(sb.toString());

				sb = new StringBuilder();
				job.getDataFile().write(sb);
				pw.write(sb.toString());

				pw.close();

				if (mListener != null) {
					mListener.onNewDocument(job);
				}
				mJobId++;

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		mRunState = 2;
	}

	public void quit() {
		mRunState = 1;

		while (mRunState != 2) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
